<?php
/**
 * 我的个人信息管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-08-03
 */
namespace Admin\Controller;

class MyinfoController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db   =new \Model\UserModel();
    }
    
    /*
     * 我的个人信息
     * 
     * @return #
     */
    public function index(){
        $myinfo=$this->db->find(session("authId"));
        $this->assign("myinfo",$myinfo);
        $this->display();
    }
    
    /*
     * 显示我的信息编辑页面
     * 
     * @return #
     */
    public function show(){
        $provinceData = M('Area')->where("id like '__0000' and state='CN'")->select();
        $this->assign ('provinceData', $provinceData);
        $my     =$this->db->find(session("authId"));
        if (!empty($my['regionid'])){
                $areaInfo = M('Area')->where(array("id"=>$my['regionid']))->find();
            }
        if ($areaInfo){
            //获取城市
            $code = substr($areaInfo['id'], 0, 2);
            $cityData = M('Area')->where("id like '{$code}__00' and state='CN' and id<>'{$code}0000'")->select();
            $this->assign ('cityData', $cityData);
            //获取地区
            $code = substr($areaInfo['id'], 0, 4);
            $areaData = M('Area')->where("id like '{$code}__' and state='CN' and id<>'{$code}00'")->select();
            $this->assign ('areaData', $areaData);
            $this->assign('areaInfo', $areaInfo);
        }
        $this->assign("userInfo",$userInfo);
        $this->assign("my",$my);
        $this->display();
    }
    
    /*
     * 编辑我的信息
     * 
     * @return #
     */
    public function myedit(){
        if(I("request.tel")){
            $map['tel'] =I("request.tel");
        }
        if(I("regionId")){
            $map['regionId']=I("request.regionId");
        }
        if(I("request.companyName")){
            if(I("request.companyName")!==session('companyName')){
                if(session("authId")!=1){
                    $map['state']       =1;
                }
                $map['companyName'] =I("request.companyName");
            }else{
                $map['companyName'] =I("request.companyName");
            }
        }else{
            $this->error("公司名称不能为空");
        }
        if(I("request.nickName")){
            $map['nickName']    =I("request.nickName");
        }else{
            $this->error("昵称不能为空");
        }
        if(I("request.websit")){
            $map['websit']  =I("request.websit");
        }
        if(I("request.sex")){
            $map['sex']     =I("request.sex");
        }
        if(I("request.province")){
            $map['regionId']    =I("post.province","","int");
        }
        if(I("request.city")){
            $map['regionId']    =I("post.city","","int");
        }
        if(I("request.addroduction")){
            $map['addroduction']=I("request.addroduction");
        }
        if(I("request.license")){
            $map['license']     =I("request.license");
        }
        if(I("request.icon")){
            $map['icon']        =I("request.icon");
        }
        $map['id']              =session("authId");
        if($this->db->myEdit($map)){            
            if(I("request.icon")){
                session("icon",I("request.icon"));
            }
            $this->success("资料修改成功");
        }else{
            $this->error("资料修改失败");
        }
    }
    
    /*
     * 上传营业执照
     * 
     * @return #
     */
    public function upyyzz(){
        if(!empty($_FILES['icon']['name'])){            
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',                              //根目录
                'savePath'  =>'userZZ/',                                            //保存路径
                'maxSize'   =>'902400',                                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['icon']);
            if(!$upload){
                echo $new_upload->getError();
            }else{
               echo $upload['savepath'].$upload['savename'];
            }           
        }
    }
}